<?php
class NumberSquered implements IteratorAggregate{
    private $start, $end;
    private $cur;

    public function __construct($start, $end){
        $this->start = $start;
        $this->end = $end;
    }

    public function getIterator(){
        return new NumberSqueredIterator($this);
    }

    public function getStart(){
        return $this->start;
    }

    public function getEnd(){
        return $this->end;
    }
}

class NumberSqueredIterator implements Iterator {
    private $cur;
    private $obj;

    public function __construct($obj){
        $this->obj = $obj;
    }
    public function rewind(){
        $this->cur = $this->obj->getStart();
    }

    public function key(){
        return $this->cur;
    }

    public function current(){
        return pow($this->cur, 2);
    }

    public function next(){
        $this->cur++;
    }

    public function valid(){
        return $this->cur <= $this->obj->getEnd();
    }
}
$obj = new NumberSquered(3, 7);
foreach($obj as $key => $value){
    print "The squre of $key is $value<br />";
}